package com.mentaleye.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.TeacherBean;
import com.mentaleye.dal.dao.TeacherDao;
import com.mentaleye.dal.model.Teacher;
import com.mentaleye.dal.model.TeacherExample;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.service.TeacherService;
import com.mentaleye.util.CommonPage;
@Service
public class TeacherServiceImpl implements TeacherService {

	@Autowired private TeacherDao teacherDao;
	
	//教工编辑：添加或修改
	@Override
	public boolean editTeacher(TeacherBean teacherBean, boolean add) {
		boolean result = false;
		Teacher teacher = tansfer(teacherBean);
		if(add){
			result = teacherDao.addTeacher(teacher);
		}else{
				result = teacherDao.updateTeacher(teacher);
		}
		return result;
	}
	//修改教工状态
	@Override
	public boolean editStatus(TeacherBean teacherBean) {
		Teacher teacher = tansfer(teacherBean);
		boolean result=false;
		result = teacherDao.updateTeacher(teacher);
		return result;
	}
	
	@Override
	public List<TeacherBean> getAllTeachers(TeacherExample  teacherExample) {
		teacherExample.createCriteria().andTeacherNoIsNotNull();
		List<TeacherBean> teacherList = tansfer(teacherDao.queryTeacherList(teacherExample));
		return teacherList;
	}

	@Override
	public List<TeacherBean> getTeacherByName(String teacherName) {
		TeacherExample teacherExample = new TeacherExample();
		teacherExample.createCriteria().andTeacherNameEqualTo(teacherName);
		List<TeacherBean> teacher = tansfer(teacherDao.queryTeacherList(teacherExample));
		return teacher;
	}

	//删除教工
	@Override
	public boolean deleteTeacherByList(List<Integer> ids) {
		boolean result = false;
		if(ids.size()>0){
			Teacher teacher = new Teacher();
			teacher.setTeacherStatus(DictionaryEnums.HAS_DELETE.getCode());
			result = teacherDao.updateTeacherStatus(ids, teacher);
		}
		return result;
	}
	
	public List<TeacherBean> tansfer(List<Teacher> teachers){
		List<TeacherBean> teacherBeans = new ArrayList<TeacherBean>();
		if(teachers==null)
			return null;
		for(Teacher teacher:teachers){
			teacherBeans.add(tansfer(teacher));
		}
		return teacherBeans;
	}
	
	public TeacherBean tansfer(Teacher teacher){
		TeacherBean teacherBean = new TeacherBean();
		teacherBean.setAttendanceCardId(teacher.getAttendanceCardId());
		teacherBean.setCreateTime(teacher.getCreateTime());
		teacherBean.setImmediateSupervisor(teacher.getImmediateSupervisor());
		teacherBean.setModifyTime(teacher.getModifyTime());
		teacherBean.setTeacherAddress(teacher.getTeacherAddress());
		teacherBean.setTeacherIdCard(teacher.getTeacherIdCard());
		teacherBean.setTeacherName(teacher.getTeacherName());
		teacherBean.setTeacherNo(teacher.getTeacherNo());
		teacherBean.setTeacherPosition(teacher.getTeacherPosition());
		teacherBean.setTeacherQq(teacher.getTeacherQq());
		teacherBean.setTeacherSex(teacher.getTeacherSex());
		teacherBean.setTeacherStatus(teacher.getTeacherStatus());
		teacherBean.setTeacherTelephone(teacher.getTeacherTelephone());
		teacherBean.setTeacherEducation(teacher.getTeacherEducation());
		teacherBean.setTeacherProfile(teacher.getTeacherProfile());
		teacherBean.setJobNumber(teacher.getJobNumber());
		teacherBean.setDept(teacher.getDept());
		return teacherBean;
	}
	public Teacher tansfer(TeacherBean teacherBean){
		Teacher teacher = new Teacher();
		teacher.setAttendanceCardId(teacherBean.getAttendanceCardId());
		teacher.setCreateTime(teacherBean.getCreateTime());
		teacher.setImmediateSupervisor(teacherBean.getImmediateSupervisor());
		teacher.setModifyTime(teacherBean.getModifyTime());
		teacher.setTeacherAddress(teacherBean.getTeacherAddress());
		teacher.setTeacherIdCard(teacherBean.getTeacherIdCard());
		teacher.setTeacherName(teacherBean.getTeacherName());
		teacher.setTeacherNo(teacherBean.getTeacherNo());
		teacher.setTeacherPosition(teacherBean.getTeacherPosition());
		teacher.setTeacherQq(teacherBean.getTeacherQq());
		teacher.setTeacherSex(teacherBean.getTeacherSex());
		teacher.setTeacherStatus(teacherBean.getTeacherStatus());
		teacher.setTeacherTelephone(teacherBean.getTeacherTelephone());
		teacher.setTeacherEducation(teacherBean.getTeacherEducation());
		teacher.setTeacherProfile(teacherBean.getTeacherProfile());
		teacher.setJobNumber(teacherBean.getJobNumber());
		teacher.setDept(teacherBean.getDept());
		return teacher;
	}

	@Override
	public TeacherBean getTeacherByNo(int teacherNo) {
		Teacher teacher = new Teacher();
		teacher =  teacherDao.queryTeacherByNo(teacherNo);
		TeacherBean teacherBean = tansfer(teacher);
		return teacherBean;
	}
	//查询所有教师、分页
	@Override
	public void queryTeacherByPage(CommonPage commonPage) {
		List<TeacherBean> data= tansfer(teacherDao.queryTeacher(commonPage.pageToMap()));
		commonPage.setPageData(data);
	}
	
	@Override
	public void queryTeacherByDept(CommonPage commonPage) {
		List<TeacherBean> data= tansfer(teacherDao.queryTeacherByDept(commonPage.pageToMap()));
		commonPage.setPageData(data);
	}
	
	@Override
	public List<TeacherBean> getTeacherListByIds(List<Integer> ids) {
		List<Teacher> teachers = teacherDao.queryTeacherByIds(ids);
		List<TeacherBean> list = tansfer(teachers);
		return list;
	}
	
	@Override
	public List<TeacherBean> queryTeacherByNotOaClass(Integer personRole){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("personRole", personRole);
		List<TeacherBean> data = tansfer(teacherDao.queryTeacherByNotOaClass(map));
		return data;
	}
	@Override
	public List<TeacherBean> getTeacherListByNotInIds(List<Integer> ids) {
		List<Teacher> teachers = teacherDao.queryTeacherByNotInIds(ids);
		List<TeacherBean> list = tansfer(teachers);
		return list;
	}
	
	
	@Override
	public List<TeacherBean> queryTeacher(){
		return tansfer(teacherDao.queryTeacher());
	}
	@Override
	public void queryRoleTeacherByPage(CommonPage commonPage) {
		List<Teacher> teacher = teacherDao.queryRoleTeacher(commonPage.pageToMap());
		List<TeacherBean> data= tansfer(teacher);
		commonPage.setPageData(data);
	}
	@Override
	public List<TeacherBean> queryAllTeacherByRoleNo(Integer id) {
		return tansfer(teacherDao.queryAllTeacherByMap(id));
	}
	@Override
	public List<TeacherBean> queryTeacherNoDel() {
		return tansfer(teacherDao.queryAllTeacherNotDel());
	}
	@Override
	public List<TeacherBean> getTeacherByExample(TeacherExample teacherExample) {
		List<TeacherBean> teacherList = tansfer(teacherDao.queryTeacherList(teacherExample));
		return teacherList;
	}

}
